package et.item.drill;

import ic2.api.item.ElectricItem;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.world.World;
import thaumcraft.api.aspects.Aspect;
import thaumcraft.common.config.ConfigItems;
import thaumcraft.common.items.wands.ItemWandCasting;

/**
 * Created by winston_wang on 2017/6/14.
 */
public class ETItemBurnDrill extends ETItemDrill {
    public ETItemBurnDrill(){
        super(8.0f, 80.0f, 57000, 100, 1, 8,17.0f);
        setTextureName("et:itemVoidFireDrill");
        setUnlocalizedName("burnVoidDrill");
    }
    @Override
    public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, Block block, int par4, int par5, int par6, EntityLivingBase par7EntityLiving) {
        if((double)block.getBlockHardness(par2World, par4, par5, par6) != 0.0D) {
            if(par7EntityLiving != null) {
                if (par2World.isRemote) {
                    return true;
                }
                ElectricItem.manager.use(par1ItemStack, this.operationEnergyCost, par7EntityLiving);
                Object obj = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(block,1,par2World.getBlockMetadata(par4,par5,par6)));
                if (obj != null) {
                    par2World.setBlockToAir(par4,par5,par6);
                    EntityItem entityItem = new EntityItem(par2World, par4, par5, par6, ((ItemStack) obj).copy());
                    par2World.spawnEntityInWorld(entityItem);
                }
                if (((EntityPlayer)par7EntityLiving).inventory.hasItem(ConfigItems.itemWandCasting)){
                    for(ItemStack itemStack : ((EntityPlayer)par7EntityLiving).inventory.mainInventory){
                        if (itemStack!=null
                                &&itemStack.getItem().equals(ConfigItems.itemWandCasting)){
                            ((ItemWandCasting)itemStack.getItem()).addRealVis(itemStack, Aspect.FIRE,100,true);
                        }
                    }
                }
            } else {
                ElectricItem.manager.discharge(par1ItemStack, this.operationEnergyCost, this.tier, true, false, false);
            }
        }
        return super.onBlockDestroyed(par1ItemStack,par2World, block,par4,par5,par6,par7EntityLiving);
    }
    @Override
    public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) {
        ElectricItem.manager.use(stack, this.operationEnergyCost, attacker);
        target.setFire(target.worldObj.rand.nextInt(5) + 1);
        return true;
    }
}
